package admin

import (
	"pfl/db"
	"pfl/models"
	"strconv"
	"strings"
	"time"

	"github.com/astaxie/beego/context"
)

const (
	L_Login = iota
	L_Logout
	L_AddUser
	L_EditUser
	L_ForbiddenUser
	L_DeleteUser
	L_ChangePwd
	L_AddArtice
	L_EditArtice
	L_DeleteArtice
	L_ChanetCarousel
)

var Log_Map = map[int]string{
	L_Login:          "用户登录",
	L_Logout:         "用户注销",
	L_AddUser:        "添加用户",
	L_EditUser:       "编辑用户资料",
	L_ForbiddenUser:  "禁用用户",
	L_DeleteUser:     "删除用户",
	L_ChangePwd:      "改变密码",
	L_AddArtice:      "添加新闻",
	L_EditArtice:     "编辑新闻",
	L_DeleteArtice:   "删除新闻",
	L_ChanetCarousel: "替换轮播图",
}

func InsertOperationLog(ctx *context.Context, srcid int, targetid int, operation int) {
	var Opa string
	if v, ok := Log_Map[operation]; ok {
		Opa = v
	} else {
		return
	}
	if srcid == 0 {
		str := ctx.Input.Session(SessionKey)
		strs := strings.Split(str.(string), ":")
		srcid, _ = strconv.Atoi(strs[1])
	}
	O := &models.Operationlog{
		Srcid:         srcid,
		Targetid:      targetid,
		Operationtime: time.Now(),
		Operation:     Opa,
		Host:          ctx.Input.Host(),
	}
	db.Mysql.InsertOne(O)
}
